# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libarchive
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.8.0
pkgrel=1
pkgdesc="Multi-format archive and compression library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.libarchive.org/"
msys2_repository_url='https://github.com/libarchive/libarchive/'
msys2_references=(
  "cpe: cpe:/a:libarchive:libarchive"
)
license=("BSD")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
depends=(${MINGW_PACKAGE_PREFIX}-gcc-libs
         ${MINGW_PACKAGE_PREFIX}-bzip2
         ${MINGW_PACKAGE_PREFIX}-expat
         ${MINGW_PACKAGE_PREFIX}-libb2
         ${MINGW_PACKAGE_PREFIX}-libiconv
         ${MINGW_PACKAGE_PREFIX}-lz4
         ${MINGW_PACKAGE_PREFIX}-libsystre
         ${MINGW_PACKAGE_PREFIX}-openssl
         ${MINGW_PACKAGE_PREFIX}-xz
         ${MINGW_PACKAGE_PREFIX}-zlib
         ${MINGW_PACKAGE_PREFIX}-zstd)
source=("https://github.com/libarchive/libarchive/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.xz")
sha256sums=('67bfac3798a778143f4b1cadcdb3792b4269486f8e1b70ca5c0ee5841398bfdf')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  autoreconf -fiv
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  if [ "${CARCH}" = "i686" ]; then
    # Need for 32-bit toolchain to have 64bit time
    CFLAGS+=" -D__MINGW_USE_VC2005_COMPAT"
  fi

  # Mention default options explicitly to clarify any confusion
  ../${_realname}-${pkgver}/configure \
    --build="${MINGW_CHOST}" \
    --host="${MINGW_CHOST}" \
    --target="${MINGW_CHOST}" \
    --prefix="${MINGW_PREFIX}" \
    --without-xml2 \
    --without-lzo2 \
    --without-nettle \
    --with-openssl \
    --with-cng

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  make check
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install
 
  #fix issue with hard-coded path in libarchive.pc file.
  _localpath=$(cygpath -wm ${MINGW_PREFIX})
  sed -i -e "s|${_localpath}|${MINGW_PREFIX}|g" "${pkgdir}"/${MINGW_PREFIX}/lib/pkgconfig/libarchive.pc
}
